package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.CrossNewMonopolyMarketUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossNewMonopolyMarketUserDataDaoImpl extends TempDataObjectDao<CrossNewMonopolyMarketUserData> {

    public static String getCrossNewMonopolyMarketUserDataSql = "select * from `t_c_new_monopoly_market_user_data` where `weekId`=? and `groupId`=?";
    public Map<Long, Map<Long, CrossNewMonopolyMarketUserData>> getCrossNewMonopolyMarketUserData(int weekId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Long, CrossNewMonopolyMarketUserData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossNewMonopolyMarketUserDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossNewMonopolyMarketUserData data = parse(rs);
                if(!map.containsKey(data.getServerId())){
                    map.put(data.getServerId(), new ConcurrentHashMap<>());
                }
                map.get(data.getServerId()).put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossNewMonopolyMarketUserDataSql = "update `t_c_new_monopoly_market_user_data` set serverId=?,resGetNum=?,lastUpdate=?,getUserReward=?,getServerReward=? where weekId=? and groupId=? and userId=?";
    @Override
    protected boolean updateData(CrossNewMonopolyMarketUserData crossNewMonopolyMarketUserData) {
        crossNewMonopolyMarketUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getResGetNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getLastUpdate()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossNewMonopolyMarketUserData.isGetUserReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossNewMonopolyMarketUserData.isGetServerReward()));
        para.put(i++, new DbParameter(Types.INTEGER, crossNewMonopolyMarketUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossNewMonopolyMarketUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossNewMonopolyMarketUserDataSql = "insert into `t_c_new_monopoly_market_user_data`(weekId,groupId,userId,serverId,resGetNum,lastUpdate,getUserReward,getServerReward) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossNewMonopolyMarketUserData crossNewMonopolyMarketUserData) {
        crossNewMonopolyMarketUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossNewMonopolyMarketUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getResGetNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossNewMonopolyMarketUserData.getLastUpdate()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossNewMonopolyMarketUserData.isGetUserReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossNewMonopolyMarketUserData.isGetServerReward()));
        if (getDb().executeNoneQuery(insertCrossNewMonopolyMarketUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossNewMonopolyMarketUserData parseData(ResultSet rs) throws SQLException {
        CrossNewMonopolyMarketUserData crossNewMonopolyMarketUserData = new CrossNewMonopolyMarketUserData();
        crossNewMonopolyMarketUserData.setWeekId(rs.getInt("weekId"));
        crossNewMonopolyMarketUserData.setGroupId(rs.getLong("groupId"));
        crossNewMonopolyMarketUserData.setUserId(rs.getLong("userId"));
        crossNewMonopolyMarketUserData.setServerId(rs.getLong("serverId"));
        crossNewMonopolyMarketUserData.setResGetNum(rs.getLong("resGetNum"));
        crossNewMonopolyMarketUserData.setLastUpdate(rs.getLong("lastUpdate"));
        crossNewMonopolyMarketUserData.setGetUserReward(rs.getBoolean("getUserReward"));
        crossNewMonopolyMarketUserData.setGetServerReward(rs.getBoolean("getServerReward"));

        return crossNewMonopolyMarketUserData;
    }
}
